﻿'use strict';
app.controller('BitCoinController', function ($scope, $rootScope, $stateParams, $localStorage, $state, $filter, $timeout, $modal, PaymentFactory, BitcoinFactory) {
    $scope.user = $localStorage.user,
    $scope.perfectmoney = {},
    $scope.title = "Bitcoin Payment",
    $scope.messeger = "",
    $scope.processing = false;
    $scope.popup = function (windowClass, size) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "Modal",
            controller: "PaymentModalInstanceCtrl",
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    };         
    $scope.showFormBitcoin = true;
    $scope.processing = false,
    $scope.showQRCode = false,
    $scope.PinRate = 0,
    $scope.BitcoinAccount = {
        Amount: $stateParams.amount,
        Address: '',
        RateUSD: 0,
        BCTransactionId: 0,
        Data: function () { return "bitcoin:" + this.Address + "?amount=" + this.RateUSD * this.Amount }
    },
    GetPinRate(),
    GetBitcointoUSDRate(1),
    getAccountBitCoin($localStorage.user.UserId, $stateParams.amount, $scope.BitcoinAccount.BCTransactionId),
    $scope.checkPaymentBitcoin = function (Id) {
        $scope.processing = true,
        BitcoinFactory.CheckBitcoindPayment(Id).then(function successCallback(resp) {
            $scope.processing = false,
            $scope.title = "Check Payment",
            $scope.messeger = resp.data.Messeger;
            if (resp.data.Status)
            {
                $state.go('app.wallethistory', { WalletId: 1 });
            } else {
                $scope.popup('modal-message modal-warning', 'sm');
            }

        }, function (error) {
            $scope.processing = false,
            $scope.title = "Check Payment error",
            $scope.messeger = 'Unable to load status payment bitcoin data: ' + error.message;
            $scope.popup('modal-message modal-danger', 'sm');
        });
    },
    $scope.doInput = function (data) {
        $scope.processing = true;
        if (data.Amount > 0 && data.Address != '' && data.BCTransactionId) {
            $scope.showQRCode = true;
            getAccountBitCoin($localStorage.user.UserId, data.Amount, $scope.BitcoinAccount.BCTransactionId);
        } else {
            $scope.showQRCode = false;
        }
    };
    function getAccountBitCoin(UserId, Amount, Id) {
        BitcoinFactory.getAccountBitcoinForPin(UserId, Amount, Id).then(
            function successCallback(resp) {
                if (resp.data.Status) {
                    $scope.processing = false;
                    $scope.BitcoinAccount.Address = resp.data.Result;
                    $scope.BitcoinAccount.BCTransactionId = resp.data.BCTransactionId;
                    $scope.showQRCode = true;
                } else {
                    $scope.messeger = resp.data.Messeger;
                }

            }, function errorCallback(resp) {
            })
    }
    function GetPinRate() {
        commonFactory.GetPinRate().then(function (response) {
            $scope.PinRate = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    }
    function GetBitcointoUSDRate(Bitcoin) {
        BitcoinFactory.GetBitcointoUSDRate(Bitcoin).then(function (response) {
            $scope.BitcoinAccount.RateUSD = response.data;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    }
});

app.controller('PaymentModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
